{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# STIL Integration\n",
    "\n",
    "[STIL](http://www.star.bristol.ac.uk/~mbt/stil/), the Starlink Tables Infrastructure Library, is a Java API for working with astronomical data, including VOTable, FITS, SQL, ASCII, CSV, CDF, and GBIN formats.  This notebook shows how to load STIL, and configure BeakerX to display STIL StarTables with the BeakerX interactive table widget."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%classpath add mvn commons-io commons-io 2.6\n",
    "\n",
    "import org.apache.commons.io.FileUtils\n",
    "\n",
    "stilUrl = \"http://www.star.bristol.ac.uk/~mbt/stil/stil.jar\"\n",
    "stilFile = System.getProperty(\"java.io.tmpdir\") + \"/stilFiles/stil.jar\"\n",
    "FileUtils.copyURLToFile(new URL(stilUrl), new File(stilFile));\n",
    "\n",
    "%classpath add dynamic stilFile\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import uk.ac.starlink.table.StarTable\n",
    "import uk.ac.starlink.table.Tables\n",
    "import jupyter.Displayer\n",
    "import jupyter.Displayers\n",
    "\n",
    "Displayers.register(StarTable.class, new Displayer<StarTable>() {\n",
    "        \n",
    "    def getColumnNames(t){\n",
    "        names = []\n",
    "        nCol = t.getColumnCount();\n",
    "        for ( int icol = 0; icol < nCol; icol++ ) {\n",
    "            names.add(t.getColumnInfo(icol).getName())\n",
    "        }\n",
    "        names   \n",
    "    }\n",
    "    \n",
    "    @Override\n",
    "    public Map<String, String> display(StarTable table) {\n",
    "        \n",
    "        columnNames = getColumnNames(table)\n",
    "        columnInfos = Tables.getColumnInfos(table)\n",
    "        MAXCHAR = 64\n",
    "        \n",
    "        new TableDisplay(\n",
    "                (int)table.getRowCount(),\n",
    "                (int)table.getColumnCount(),\n",
    "                columnNames,\n",
    "                new TableDisplay.Element() {\n",
    "                  @Override\n",
    "                  public String get(int columnIndex, int rowIndex) {\n",
    "                    Object cell = table.getCell(rowIndex, columnIndex);\n",
    "                    return columnInfos[columnIndex].formatValue(cell, MAXCHAR)\n",
    "                  }\n",
    "                }\n",
    "        ).display();\n",
    "        return OutputCell.DISPLAYER_HIDDEN;\n",
    "      }\n",
    "    });"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import org.apache.commons.io.FileUtils\n",
    "\n",
    "messierUrl = \"http://andromeda.star.bristol.ac.uk/data/messier.csv\"\n",
    "messierFile = System.getProperty(\"java.io.tmpdir\") + \"/stilFiles/messier.csv\"\n",
    "FileUtils.copyURLToFile(new URL(messierUrl), new File(messierFile));\n",
    "\"Done\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import uk.ac.starlink.table.StarTable\n",
    "import uk.ac.starlink.table.StarTableFactory\n",
    "import uk.ac.starlink.table.Tables\n",
    "\n",
    "starTable = new StarTableFactory().makeStarTable( messierFile, \"csv\" );\n",
    "starTable = Tables.randomTable(starTable)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Groovy",
   "language": "groovy",
   "name": "groovy"
  },
  "language_info": {
   "codemirror_mode": "groovy",
   "file_extension": ".groovy",
   "mimetype": "",
   "name": "Groovy",
   "nbconverter_exporter": "",
   "version": "2.4.3"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
